package com.dianyingyuyue.controller;

import com.dianyingyuyue.entity.User;
import com.dianyingyuyue.service.UserService;
import com.dianyingyuyue.util.Result;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@WebServlet(name = "LoginServlet",urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type=request.getParameter("type");
        String method=request.getParameter("method");
        request.setCharacterEncoding("utf-8");
        UserService service=new UserService();
        if(method.equals("login")){
            //这里类型要设置未text/json
            response.setContentType("text/json;charset=utf-8");
            String username=request.getParameter("username");
            String pwd=request.getParameter("pwd");
            String utype=request.getParameter("utype");
            List<Object> objects=new ArrayList<>();
            objects.add(username);
            objects.add(pwd);
            objects.add(utype);
            User db=service.login(objects);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            if(db!=null){//登录成功
                request.getSession().setAttribute("user",db);
                writer.write(new Gson().toJson(Result.reOk("登录成功")));
            }else{//账号密码错误
                writer.write(new Gson().toJson(Result.reError("账号密码错误")));
            }
        }else if(method.equals("register")){
            //这里类型要设置未text/json
            response.setContentType("text/json;charset=utf-8");
            String username=request.getParameter("username");
            String pwd=request.getParameter("pwd");
            String sex=request.getParameter("sex");
            String phone=request.getParameter("phone");
            String utype=request.getParameter("utype");
            List<Object> objects=new ArrayList<>();
            objects.add(username);
            objects.add(pwd);
            objects.add(sex);
            objects.add(phone);
            objects.add(utype);
            int count=service.add(objects);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            if(count==1){//注册成功
                writer.write(new Gson().toJson(Result.reOk("注册成功")));
            }else{//注册失败
                writer.write(new Gson().toJson(Result.reError("注册失败")));
            }
        }else if(method.equals("get")){//获取当前账号
            response.setContentType("text/json;charset=utf-8");
            PrintWriter writer = response.getWriter();
            if(request.getSession().getAttribute("user")==null){
                writer.write(new Gson().toJson(Result.reError("请先登录")));
            }else{
                User user=(User)request.getSession().getAttribute("user");
                User user1=service.getId(user.getId());
                // 创建一个日期格式化对象，设置为中文
                SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
                // 创建一个Gson对象，设置日期格式化
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").setPrettyPrinting()
                        .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (src, typeOfSrc, context) -> new JsonPrimitive(chineseDateFormat.format(src)))
                        .create();
                writer.write(gson.toJson(Result.reOk("",user1)));
            }
        }else if(method.equals("up")){//修改
            response.setContentType("text/json;charset=utf-8");
            String username=request.getParameter("username");
            String pwd=request.getParameter("pwd");
            String sex=request.getParameter("sex");
            String phone=request.getParameter("phone");
            String id=request.getParameter("id");
            List<Object> objects=new ArrayList<>();
            objects.add(username);
            objects.add(pwd);
            objects.add(sex);
            objects.add(phone);
            objects.add(id);
            int count=service.up(objects);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            if(count==1){//注册成功
                writer.write(new Gson().toJson(Result.reOk("修改成功")));
            }else{//注册失败
                writer.write(new Gson().toJson(Result.reError("修改失败")));
            }
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request,response);
    }


}
